package cn.edu.java.thread.原子类;

import java.util.concurrent.atomic.AtomicReferenceArray;

public class AtomicReferenceArrayDemo {
    public static void main(String[] args) {
        // 创建AtomicReferenceArray对象，并指定数组长度为5
        AtomicReferenceArray<String> array = new AtomicReferenceArray<>(5);

        // 设置数组元素
        array.set(0, "Java");
        array.set(1, "Python");
        array.set(2, "C++");
        array.set(3, "JavaScript");
        array.set(4, "Ruby");

        // 获取指定索引位置的元素
        String element = array.get(2);
        System.out.println("Element at index 2: " + element);

        // 更新指定索引位置的元素
        array.compareAndSet(1, "Python", "Go");

        // 遍历数组并输出元素
        for (int i = 0; i < array.length(); i++) {
            System.out.println("Element at index " + i + ": " + array.get(i));
        }
    }
}

